import sys,string,math

def computeAveragesAndStandardErrors( inFile, outFile ):
	fIn = open(inFile,'r')
	fOut = open(outFile,'w')
	for line in fIn:
		val = line.split()
		x = float(val[0]);
		p2_1 = float(val[1]);
		p2_2 = float(val[2]);
		p2_3 = float(val[3]);
		p2_4 = float(val[4]);
		sumy = p2_1 + p2_2 + p2_3 + p2_4 
		avgy = float(sumy/4) 
		sqP2_1 = float((p2_1)**(2))
		sqP2_2 = float((p2_2)**(2))
		sqP2_3 = float((p2_3)**(2))
		sqP2_4 = float((p2_4)**(2))
		sum2 = float((sqP2_1+sqP2_2+sqP2_3+sqP2_4))
		sumsq = float(((p2_1+p2_2+p2_3+p2_4)**(2))/4)
		sdy = float(((sum2-sumsq)/2)**(1/2.0))
		sey = float((sdy)/((2)**(1/2.0)))
		print >>fOut, x,"\t",avgy,"\t",sey
	fIn.close()
	fOut.close()
		
# calculate averages and standard errors for p2x
# first step: collect all values in a single file p2xAll.dat
o = open('p2xAll.dat','w') 
f1 = open('analysisSet1/P2.dat','r')
f2 = open('analysisSet2/P2.dat','r')
f3 = open('analysisSet3/P2.dat','r')
f4 = open('analysisSet4/P2.dat','r')
while 1: # Create infinite loop
        line1 = f1.readline() # Read each line - data MUST begin at line 0.
        line2 = f2.readline()
	line3 = f3.readline()
        line4 = f4.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        print >> o, words1[0], words1[1], words2[1], words3[1], words4[1] # Print each value
f1.close()
f2.close()
f3.close()
f4.close()
o.close()
# second step: calculate actual avgs and SEs
computeAveragesAndStandardErrors( 'p2xAll.dat', 'avgP2x.dat' )

# calculate averages and standard errors for p2y
# first step: collect all values in a single file p2yAll.dat
o = open('p2yAll.dat','w') 
f1 = open('analysisSet1/P2.dat','r')
f2 = open('analysisSet2/P2.dat','r')
f3 = open('analysisSet3/P2.dat','r')
f4 = open('analysisSet4/P2.dat','r')
while 1: # Create infinite loop
        line1 = f1.readline() # Read each line - data MUST begin at line 0.
        line2 = f2.readline()
	line3 = f3.readline()
        line4 = f4.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        print >> o, words1[0], words1[2], words2[2], words3[2], words4[2] # Print each value
f1.close()
f2.close()
f3.close()
f4.close()
o.close()
# second step: calculate actual avgs and SEs
computeAveragesAndStandardErrors( 'p2yAll.dat', 'avgP2y.dat' )

# calculate averages and standard errors for p2z
# first step: collect all values in a single file p2zAll.dat
o = open('p2zAll.dat','w') 
f1 = open('analysisSet1/P2.dat','r')
f2 = open('analysisSet2/P2.dat','r')
f3 = open('analysisSet3/P2.dat','r')
f4 = open('analysisSet4/P2.dat','r')
while 1: # Create infinite loop
        line1 = f1.readline() # Read each line - data MUST begin at line 0.
        line2 = f2.readline()
	line3 = f3.readline()
        line4 = f4.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        print >> o, words1[0], words1[3], words2[3], words3[3], words4[3] # Print each value
f1.close()
f2.close()
f3.close()
f4.close()
o.close()
# second step: calculate actual avgs and SEs
computeAveragesAndStandardErrors( 'p2zAll.dat', 'avgP2z.dat' )
